Java syntax
part 25/46 · 86.7 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
By default, all methods in all classes are concrete, unless the abstract keyword is used. An abstract class may include abstract methods, which have no implementation. By default, all methods in all interfaces are abstract, unless the default keyword is used. The default keyword can be used to specify a concrete method in an interface.
//By default, all methods in all classes are concrete, unless the abstract keyword is used.
public abstract class Demo {
// An abstract class may include abstract methods, which have no implementation.
public abstract int sum(int x, int y);
// An abstract class may also include concrete methods.
public int product(int x, int y) {
return x*y;
}
}
//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface {
int getLength(); //The abstract keyword can be used here, though is completely useless
//The default keyword can be used in this context to specify a concrete method in an interface
default int product(int x, int y) {
return x * y;
}
}
Final class
A final class cannot be subclassed. As doing this can confer security and efficiency benefits, many of the Java standard library classes are final, such as java.lang.System and java.lang.String.
Example:
public final class MyFinalClass {...}
public class ThisIsWrong extends MyFinalClass {...} // forbidden
Access modifiers